﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.TeamFoundation.Build.Client;
using System.Diagnostics;
using System.IO;

namespace TeamDeploy.Activities
{
    [BuildActivity(HostEnvironmentOption.Agent)]
    public sealed class KillProcess : CustomActivityBase
    {
        [RequiredArgument]
        public InArgument<string> ProcessName { get; set; }
        
        [RequiredArgument]
        public InArgument<string> TargetMachine { get; set; }
        
        [RequiredArgument]
        public InArgument<string> KillAppPathFilename { get; set; }

        public InArgument<string> IgnoreExitCode { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string processName = context.GetValue(this.ProcessName);
            string targetMachine = context.GetValue(this.TargetMachine);
            string killAppPathFilename = context.GetValue(this.KillAppPathFilename);
            bool ignoreExitCode = false;
            bool.TryParse(context.GetValue(this.IgnoreExitCode), out ignoreExitCode);

            if (!File.Exists(killAppPathFilename))
            {
                 throw new ArgumentException(
                   "KillApp does not exist at", "KillAppPathFilename");
            }

            string args = "\\\\" + targetMachine + " -accepteula " + processName;
            //Process p = Process.Start(killAppPathFilename, args);
            //p.WaitForExit();

            using (System.Diagnostics.Process process = new System.Diagnostics.Process())
            {
                process.StartInfo.FileName = killAppPathFilename;
                //process.StartInfo.WorkingDirectory = SearchPathRoot.Get(context);                    
                process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.ErrorDialog = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.Arguments = args;

                try
                {
                    process.Start();
                    string output = process.StandardOutput.ReadToEnd();
                    process.WaitForExit();
                    LogMessage(context, "KillProcess Output:" + output);
                }
                catch (Exception ex)
                {
                    LogMessage(context, ex.ToString());
                    throw ex;
                }

                if (process.ExitCode != 0)
                {
                    string message = "Process " + processName + " on " + targetMachine + " returned code:" + process.ExitCode;

                    if (ignoreExitCode)
                    {
                        LogMessage(context, message);
                    }
                    else
                    {
                        LogMessage(context, message);
                        //  return false;
                    }
                }
            }

        }
    }
}
